﻿using Blog.Core.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Core.Data
{
    /// <summary>
    /// 数据库种子
    /// </summary>
    public static class DbInitializer
    {
        public static async void Initialize(BlogContext context)
        {
            context.Database.EnsureCreated();

            // Look for any students.
            if (context.Students.Any())
            {
                return;   // DB has been seeded
            }
            var roles = new Role[]{
                new Role {Name="admin"},
                new Role {Name = "default"},
                new Role {Name = "guest"}
            };
            var userroles = new UserRole[]{
            new UserRole { User = new User { UserName = "shang",Password ="shang123" }, Role = roles[0] },
            new UserRole { User = new User { UserName = "shang2" }, Role = roles[1] },
            new UserRole { User = new User { UserName = "admin", Password = "123456", Email = "123@qq.com", Phone = "15116228315" }, Role = roles[0] },
            new UserRole { User = new User { UserName = "guest", Password = "123456" }, Role = roles[1] }
            };
            var us =new List<UserRole>();
            for (int i = 0; i < 100; i++)
            {
                us.Add(new UserRole { User = new User(), Role = roles[1] });
            }
            // startTask
            var addUserRoles = context.UserRoles.AddRangeAsync(userroles);
            var addUserRoles2 = context.UserRoles.AddRangeAsync(us);



            var submenus = new SubMenu[]{
            new SubMenu{Name ="用户列表",Path="users"},
            };
            var authSubmenus = new SubMenu[]{
            new SubMenu{Name ="角色列表",Path="roles"},
            new SubMenu{Name ="权限列表",Path="authority"},
            };
            var goodsSubmenus = new SubMenu[]{
            new SubMenu{Name ="商品列表",Path="goods"},
            new SubMenu{Name ="分类参数",Path="classify"},
            };
            var menus = new MainMenu[]{
            new MainMenu{Name = "用户管理",SubMenus = submenus,IconName = "icon-team"},
            new MainMenu{Name = "权限管理", SubMenus = authSubmenus,IconName = "icon-anquanyinsi"},
            new MainMenu{Name = "商品管理",SubMenus = goodsSubmenus, IconName = "icon-maijiagouwuche"},
            new MainMenu{Name = "订单管理",IconName = "icon-dingdan"},
            new MainMenu{Name = "数据统计",IconName = "icon-tubiaozhutu"},

            };
            // startTask
            Task addmenu = context.MainMenus.AddRangeAsync(menus);


            var students = new Student[]
            {
            new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
            new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
            new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
            new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
            new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
            new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
            new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
            new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
            };
            // startTask
            Task addStudents = context.Students.AddRangeAsync(students);


            var pupils = new Pupil[]
            {
            new Pupil{FazherName="Shang", FirstMidName="Carson1",LastName="Alexander1",EnrollmentDate=DateTime.Parse("2005-09-11")},
            };
            foreach (Pupil s in pupils)
            {
                context.Pupils.Add(s);
            }
            // startTask
            var taskAddPupils = context.Pupils.AddRangeAsync(pupils);


            var courses = new Course[]
            {
            new Course{CourseID=1050,Title="Chemistry",Credits=3},
            new Course{CourseID=4022,Title="Microeconomics",Credits=3},
            new Course{CourseID=4041,Title="Macroeconomics",Credits=3},
            new Course{CourseID=1045,Title="Calculus",Credits=4},
            new Course{CourseID=3141,Title="Trigonometry",Credits=4},
            new Course{CourseID=2021,Title="Composition",Credits=3},
            new Course{CourseID=2042,Title="Literature",Credits=4}
            };
            // startTask
            var taskAddCourses = context.Courses.AddRangeAsync(courses);


            var enrollments = new Enrollment[]
            {
            new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A},
            new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C},
            new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B},
            new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B},
            new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F},
            new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F},
            new Enrollment{StudentID=3,CourseID=1050},
            new Enrollment{StudentID=4,CourseID=1050},
            new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F},
            new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C},
            new Enrollment{StudentID=6,CourseID=1045},
            new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A},
            };

            var taskAddEnrollment = context.Enrollments.AddRangeAsync(enrollments);

            await Task.WhenAll(addmenu,addUserRoles, addStudents, taskAddPupils, taskAddCourses, addUserRoles2);
            context.SaveChanges();
        }
    }
}
